Delegates
A delegate is an object that can refer to a method. Thus, when you create a delegate, you are creating an object that can hold a reference to a method. Furthermore, the method can be called through this reference. Thus, a delegate can invoke the method to which it refers. delegate in C# is similar to a function pointer in C/C++.
A delegate can be thought of as an object that contains an ordered list of methods with the same signature and return type.
Delegates are types, just as classes are types. And as with classes, a delegate type must be declared before you can use it to create variables and objects of the type.
A delegate is declared using the keyword delegate.
delegate ret-type name(parameter-list);
The delegate type declaration differs from a method declaration in two ways. The delegate type declaration
• Is prefaced with the keyword delegate
• Does not have a method body
Delegate Object
A delegate is a reference type, and therefore has both a reference and an object. After a delegate type is declared, you can declare variables and create objects of the type.
Delegate type delegate variable.
Program on Delegates
using System;
delegate void visi();
class abc
{
public void put()
{
Console.WriteLine("This is put method.....");
}
public static void put1()
{
Console.WriteLine("this is put1 static method");
}
}
class sample
{
public static void Main(String[] args)
{
visi x1, x2;
abc x = new abc();
x1 = x.put;
x2 = abc.put1;
x1();
x2();
// we can create delegate like the following
x1 = new visi(x.put);
x2 = new visi(abc.put1);
x1();
x2();
}
}
Combining Delegates
All the delegates you’ve seen so far have had only a single method in their invocation lists. Delegates can be “combined” by using the addition operator. the term combining delegates might give the impression that the operand delegates are modified, they are not changed at all. In fact, delegates are immutable. After a delegate object is created, it cannot be changed.
using System;
delegate void visi();
class abc
{
public void put()
{
Console.WriteLine("This is put method.....");
}
public static void put1()
{
Console.WriteLine("this is put1 static method");
}
}
class sample
{
public static void Main(String[] args)
{
visi x1, x2;
abc x = new abc();
x1 = x.put;
x2 = abc.put1;
visi x3;
x3 = x1 + x2;
x3();
}
}
Program on adding methods to delegate
using System;
delegate void visi();
class abc
{
public void put()
{
Console.WriteLine("This is put method.....");
}
public static void put1()
{
Console.WriteLine("this is put1 static method");
}
}
class sample
{
public static void Main(String[] args)
{
visi x1;
abc x = new abc();
x1 = x.put;
x1 += abc.put1;
x1 += x.put;
x1 += x.put;
x1();
}
}
Remove Methods From a Delegate
As with adding a method to a delegate, the resulting delegate is actually a new delegate. The new delegate is a copy of the old delegate—but without the reference to the method that was removed.
The following are some things to remember when removing methods:
Program to remove methods from a delegate
using System;
delegate void visi();
class abc
{
public void put()
{
Console.WriteLine("This is put method.....");
}
public static void put1()
{
Console.WriteLine("this is put1 static method");
}
}
class sample
{
public static void Main(String[] args)
{
visi x1;
abc x = new abc();
x1 = x.put;
x1 += abc.put1;
x1 += x.put;
x1 += x.put;
x1();
Console.WriteLine("REmoving method from delegate");
x1 -= x.put;
x1();
Console.WriteLine("Revmoving 2 methods from delegate");
x1 -= abc.put1;
x1 -= x.put;
x1();
}
}
Parameterized delegate
The parameters used to invoke the delegate are used to invoke each of the methods on the invocation list. Invoking the delegate with a parameter causes it to invoke each of the members in its invocation list with the same parameter value. A method can be in the invocation list more than once. If that is the case, then when the delegate is invoked, the method will be called each time it is encountered in the list.
Program on parameterized delegate
using System;
delegate void visi(int x);
class abc
{
public void put(int x)
{
int f = 1;
for (int i = 1; i <= x; i++)
f = f * i;
Console.WriteLine("factorial" + f);
}
public void put1(int x)
{
for (int i = 1; i <= 20; i++)
{
Console.WriteLine(i + "*" + x + "=" + (i * x));
}
}
}
class sample
{
public static void Main(String[] args)
{
visi x1;
abc x = new abc();
x1 = x.put;
x1 += x.put1;
if (null != x1)
x1(5);
else
Console.WriteLine("Delegate is empty");
}
}
Delegates with Return Values
If a delegate has a return value and more than one method in its invocation list, the following occurs:
Program to return value from a delegate
using System;
delegate int visi(int x);
class abc
{
int a;
public int put(int x)
{
a = x+5;
return a;
}
public int put1(int x)
{
a = a + x;
return a;
}
}
class sample
{
public static void Main(String[] args)
{
visi x1;
abc x = new abc();
x1 = x.put;
x1 += x.put1;
x1+=x.put;
Console.WriteLine("Value of delegate methods" + x1(5));
x1 += x.put;
Console.WriteLine("Value after adding put again" + x1(5));
}
}
Delegates with Reference Parameters
If a delegate has a reference parameter, the value of the parameter can change upon return from one or more of the methods in the invocation list.